define("func/quicklocate", [
    "leaflet",
    "func/base",
    "control/panel",
    "plugins/mcscroll"
], function(L) {

    L.ICT.Func.add("QuickLocate", {

        Class: L.Class.extend({

            initialize: function() {
                this.menu = L.ict.app.secondmenu;
                this.menuid = 'ict_secmenu_main_location';
                this.ictmap = L.ict.app.ictmap;
                this._popPanel = null;
                this._layergroup = null;
            },

            start: function() {
                this._initUi();
                this._initEvts();
                this.menu.mainmenu.deactiveMenuExceptOne(this.menuid);
                this.menu.mainmenu.disableMenuExceptOne(this.menuid);
            },

            stop: function() {
                this.menu.mainmenu.deactiveMenu(this.menuid);
                this.menu.mainmenu.enableMenu();
                if (this._popPanel) {
                    this._popPanel.remove();
                }
                if (this.ictmap.map.hasLayer(this._layergroup)) {
                    this.ictmap.map.removeLayer(this._layergroup);
                }
            },

            _initUi: function() {
                var options = {
                    title: '经纬度定位',
                    width: 360,
                    height: 300
                };
                options.contentHTML = this._container = $(this.getContentHTML());
                var pop = this._popPanel = new L.ICT.PopPanel(options);
                pop.show();
                pop.on("popPanelRemove", function() {
                    this._popPanel = null;
                    this._container = null;
                    this.stop();
                }, this);

            },

            getContentHTML: function() {
                var html = [];
                html.push('<div class="quicklocateContainer">');
                html.push('<div class="thDiv"><table><tr><th>经度</th><th>纬度</th></tr></table></div>');
                html.push('<div class="tbDiv">');
                html.push('<table class="tbTable">');
                html.push(this.getItemHtml());
                html.push('</table>');
                html.push('</div>');
                // html.push('<div class="addBtn">添加</div>');
                // html.push('<p class="msg"></p>');
                html.push('<div class="btnDiv">');
                 html.push('<button type="button" class="btn addBtn">添加</button>');
                html.push('<button type="button" class="btn okBtn">定位</button>');
                html.push('</div>');
                html.push('</div>');
                return html.join('');
            },

            getItemHtml: function() {
                var itemhtml = [];
                itemhtml.push('<tr>');
                itemhtml.push('<td class="lng lng_d"><input type="text" value="0"></td>');
                itemhtml.push('<td class="dfm">°</td>');
                itemhtml.push('<td class="lng lng_f"><input type="text" value="0"></td>');
                itemhtml.push('<td class="dfm">\′</td>');
                itemhtml.push('<td class="lng lng_m"><input type="text" value="0"></td>');
                itemhtml.push('<td class="dfm">″</td>');
                itemhtml.push('<td class="lngWE lng_W active">W</td>');
                itemhtml.push('<td class="lngWE lng_E">E</td>');
                itemhtml.push('<td class="space">&nbsp&nbsp</td>');
                itemhtml.push('<td class="lat lat_d"><input type="text" value="0"></td>');
                itemhtml.push('<td class="dfm">°</td>');
                itemhtml.push('<td class="lat lat_f"><input type="text" value="0"></td>');
                itemhtml.push('<td class="dfm">\′</td>');
                itemhtml.push('<td class="lat lat_m"><input type="text" value="0"></td>');
                itemhtml.push('<td class="dfm">″</td>');
                itemhtml.push('<td class="latNS lat_N active">N</td>');
                itemhtml.push('<td class="latNS lat_S">S</td>');
                itemhtml.push('<td class="space">&nbsp&nbsp</td>');
                itemhtml.push('<td class="deletebtn">删除</td>');
                itemhtml.push('</tr>');
                return itemhtml.join('');
            },

            _initItemEvts: function() {
                this._container.find('.tbTable .lngWE').on('click', function(e) {
                    $(this).addClass('active').siblings('.lngWE').removeClass('active');
                });
                this._container.find('.tbTable .latNS').on('click', function(e) {
                    $(this).addClass('active').siblings('.latNS').removeClass('active');
                });
                this._container.find('.deletebtn').on('click', function(e) {
                    $(this).parent().remove();
                });
            },

            _initEvts: function() {
                var self = this;

                this._container.find('.tbDiv').mCustomScrollbar({ theme: "minimal-dark" });

                this._container.find('.addBtn').on('click', function(e) {
                    self._container.find('.tbTable').append(self.getItemHtml());
                    self._container.find('.tbDiv').mCustomScrollbar("scrollTo", "bottom", {
                        scrollEasing: "easeOut"
                    });
                    self._initItemEvts();
                });

                this._container.find('.okBtn').on('click', this._okEvts.bind(this));

                // this._container.find('.cancelBtn').on('click', function(e) {
                //     self.stop();
                // });

                this._initItemEvts();
            },

            _okEvts: function(e) {
                if (this.ictmap.map.hasLayer(this._layergroup)) {
                    this.ictmap.map.removeLayer(this._layergroup);
                }
                try {
                    var latlngs = this.getLatLngs();
                } catch (ex) {
                    L.ict.app.util.dialog.error('错误', '您输入的经纬度有错误，请重新输入！');
                    return;
                }
                if (latlngs.length < 1) {
                    L.ict.app.util.dialog.error('错误', '请添加位置点！');
                    return;
                }
                this._layergroup = L.featureGroup().addTo(this.ictmap.map);
                try {
                    if (latlngs.length === 1) {
                         this.ictmap.map.panTo(latlngs[0]);
                    } else if (latlngs.length === 2) {
                        this.createAndLocatePolyline(latlngs);
                    } else {
                        this.createAndLocatePolygon(latlngs);
                    }
                    this.createPoints(latlngs);
                } catch (ex) {
                    L.ict.app.util.dialog.error('错误', '您输入的经纬度有错误，请重新输入！');
                    return;
                }
            },

            createPoints: function(latlngs) {
                var icon = new L.DivIcon({ //顶点样式
                    iconSize: new L.Point(8, 8),
                    className: 'ict_measure_vetixicon'
                });
                for (var i = 0, len = latlngs.length; i < len; i++) {
                    var latlng = latlngs[i];
                    var lat = L.ict.app.util.tool.latlngTodfmStr(latlng.lat, 'lat');
                    var lon = L.ict.app.util.tool.latlngTodfmStr(latlng.lng, 'lng');
                    var title = '经度：' + lon + '，' + '纬度：' + lat;

                    var marker = new L.Marker(latlng, {
                        // icon: icon,
                        title: title
                    });
                    this._layergroup.addLayer(marker);
                }
            },

            createAndLocatePolyline: function(latlngs) {
                var shapeOptions = {
                    stroke: true,
                    color: '#116cc1',
                    weight: 2,
                    opacity: 1,
                    fill: false,
                    clickable: false
                };
                var ply = L.polyline(latlngs, shapeOptions);
                this._layergroup.addLayer(ply);
                this.ictmap.map.fitBounds(ply.getBounds());
            },

            createAndLocatePolygon: function(latlngs) {
                var shapeOptions = {
                    stroke: true,
                    color: '#116cc1',
                    weight: 2,
                    opacity: 1,
                    fill: false,
                    clickable: false
                };
                var ply = L.polygon(latlngs, shapeOptions);
                this._layergroup.addLayer(ply);
                this.ictmap.map.fitBounds(ply.getBounds());
            },

            getLatLngs: function() {
                var latlngs = [];
                var ts = this._container.find(".tbTable tr");
                for (var i = 0, len = ts.length; i < len; i++) {
                    var $tr = $(ts[i]);
                    var lad = $tr.find('.lat_d input').val(),
                        laf = $tr.find('.lat_f input').val(),
                        lam = $tr.find('.lat_m input').val(),
                        ns = $tr.find('.latNS.active').text(),
                        lgd = $tr.find('.lng_d input').val(),
                        lgf = $tr.find('.lng_f input').val(),
                        lgm = $tr.find('.lng_m input').val();
                    we = $tr.find('.lngWE.active').text()
                    var lat = Math.abs(parseFloat(lad)) + parseFloat(laf / 60) + parseFloat(lam / 3600),
                        lng = Math.abs(parseFloat(lgd)) + parseFloat(lgf / 60) + parseFloat(lgm / 3600);
                    lat = ns == "S" ? -lat : lat;
                    lng = we == "W" ? -lng : lng;
                    var latlng = L.latLng([lat, lng]);
                    latlngs.push(latlng);
                }
                return latlngs;
            }
        })
    });
});
